package com.animation_study.custom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;

/**
 * Created by css on 2017/12/6.
 */

public class MovePointView extends View {
    Paint paint = new Paint();
    final int lt = 0;
    final int rt = 1;
    final int lb = 2;
    final int rb = 3;
    int x = 100, y = 100;
    int dir = 2;
    int width;
    int height;
    ArrayList<MyPoint> points;

    public MovePointView(Context context) {
        this(context, null);
    }

    public MovePointView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MovePointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        height = displayMetrics.heightPixels;
        points = new ArrayList<>();
        x = (int) (Math.random() * 1000 % width);
        y = (int) (Math.random() * 1000 % height);
        dir = (int) (Math.random() * 10 % 4);
        int speed = (int) (Math.random() * 100 % 10 + 1);
        points.add(new MyPoint(new Point(x, y), dir, speed));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (MyPoint point : points) {
            canvas.drawRect(point.getPoint().x, point.getPoint().y, point.getPoint().x + 10, point.getPoint().y + 10, paint);
        }
        for (MyPoint point : points) {
            int speed = point.speed;
            switch (point.dir) {
                case lt:
                    point.getPoint().x -= speed;
                    point.getPoint().y -= speed;
                    break;
                case rt:
                    point.getPoint().x += speed;
                    point.getPoint().y -= speed;
                    break;
                case lb:
                    point.getPoint().x -= speed;
                    point.getPoint().y += speed;
                    break;
                case rb:
                    point.getPoint().x += speed;
                    point.getPoint().y += speed;
                    break;

            }
        }
        for (MyPoint point : points) {
            x = point.getPoint().x;
            y = point.getPoint().y;
            if (x <= 0 || x >= width || y <= 0 || y >= height) {
                int temp;
                temp = (int) (Math.random() * 10 % 2);
                if (x <= 0 && y <= 0) {
                    dir = lb;
                } else if (x >= width && y >= height) {
                    dir = lt;
                } else if (x <= 0) {
                    if (temp == 0) {
                        dir = rt;
                    } else {
                        dir = rb;
                    }
                } else if (y <= 0) {
                    if (temp == 0) {
                        dir = lb;
                    } else {
                        dir = rb;
                    }
                } else if (x >= width) {
                    if (temp == 0) {
                        dir = lt;
                    } else {
                        dir = lb;
                    }
                } else if (y >= height) {
                    if (temp == 0) {
                        dir = lt;
                    } else {
                        dir = rt;
                    }
                }
                point.setDir(dir);
            }

        }

        x = (int) (Math.random() * 1000 % width);
        y = (int) (Math.random() * 1000 % height);
        dir = (int) (Math.random() * 10 % 4);
        int speed = (int) (Math.random() * 100 % 10 + 1);
        points.add(new MyPoint(new Point(x, y), dir, speed));
        postInvalidateDelayed(10);

    }

    class MyPoint {
        Point point;
        int dir;
        int speed;

        public int getSpeed() {
            return speed;
        }

        public void setSpeed(int speed) {
            this.speed = speed;
        }

        public MyPoint(Point point, int dir, int speed) {
            this.point = point;
            this.dir = dir;
            this.speed = speed;
        }

        public MyPoint(Point point, int dir) {
            this.point = point;
            this.dir = dir;
        }

        public Point getPoint() {
            return point;
        }

        public void setPoint(Point point) {
            this.point = point;
        }

        public int getDir() {
            return dir;
        }

        public void setDir(int dir) {
            this.dir = dir;
        }
    }

}
